﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using DAL;

namespace BLL.BranchAnLoan
{
    public class Branchs:BaseAnLoan
    {
        #region "Constructors"
        public Branchs() { }

        public Branchs(int id, string address, string ward, string district, string phone, string city, bool isHeadOffice,string name,string number)
        {
            this.ID = id;
            this.Address = address;
            this.Ward = ward;
            this.District = district;
            this.Phone = phone;
            this.City = city;
            this.IsHeadOffice = isHeadOffice;
            this.Name = name;
            this.Number = number;
        }
        #endregion

        #region "Public Properties"
        

        public string Address { get; set; }

        public string Ward { get; set; }

        public string District { get; set; }

        public string City { get; set; }

        public string Phone { get; set; }

        public bool IsHeadOffice { get; set; }

        public string Name { get; set; }

        public string Number { get; set; }
        #endregion

        #region Get Branch From BranchsDetail
        private static Branchs GetBranchFromBranchsDetail(BranchsDetails record)
        {
            if (record == null)
                return null;
            else
                return new Branchs(record.BranchID, record.Address, record.Ward, record.District, record.Phone, record.City, record.IsHeadOffice,record.Name,record.Number);
        }

        private static List<Branchs> GetBranchFromListBranchDetail(List<BranchsDetails> recordset)
        {
            List<Branchs> branchs = new List<Branchs>();
            foreach (BranchsDetails record in recordset)
                branchs.Add(GetBranchFromBranchsDetail(record));
            return branchs;
        }
        #endregion

        #region Method
        public static Branchs GetBranch(int branchID)
        {
            Branchs branch = null;
            string key = "Branch_" + branchID.ToString();
            if (BaseAnLoan.Settings.EnableCaching && BizObject.Cache[key] != null)
                branch = (Branchs)BizObject.Cache[key];
            else
            {
                branch = GetBranchFromBranchsDetail(SiteProvider.BranchAnLoan.BranchGet(branchID));
                BaseAnLoan.CacheData(key, branch);
            }
            return branch;
        }

        public static List<Branchs> ListBranch()
        {
            List<Branchs> branchs = null;
            string key = "Branchs";
            if (BaseAnLoan.Settings.EnableCaching && BizObject.Cache[key] != null)
                branchs = (List<Branchs>)BizObject.Cache[key];
            else
            {
                List<BranchsDetails> recordset = SiteProvider.BranchAnLoan.ListBranch();
                branchs = GetBranchFromListBranchDetail(recordset);
                BaseAnLoan.CacheData(key, branchs);
            }
            return branchs;
        }

        public static Branchs GetBranhIsHeadOffice()
        {
            Branchs branch = null;
            string key = "Branch_IsHeadOffice";
            if (BaseAnLoan.Settings.EnableCaching && BizObject.Cache[key] != null)
                branch = (Branchs)BizObject.Cache[key];
            else
            {
                branch = GetBranchFromBranchsDetail(SiteProvider.BranchAnLoan.BranchGetIsHeadOffice());
                BaseAnLoan.CacheData(key, branch);
            }
            return branch;
        }

        public static int InsertBranch(string address, string ward, string district, string city, string phone, bool isHeadOffice,string name,string number)
        {
            BranchsDetails record = new BranchsDetails(0, address, ward, district, phone, city, isHeadOffice,name,number);
            int ret = SiteProvider.BranchAnLoan.InsertBranch(record);
            BizObject.PurgeCacheItems("Branch");
            return ret;
        }

        public static bool UpdateBranch(int id, string address, string ward, string district, string city, string phone, bool isHeadOffice,string name,string number)
        {
            BranchsDetails record = new BranchsDetails(id, address, ward, district, phone, city, isHeadOffice,name,number);
            bool ret = SiteProvider.BranchAnLoan.UpdateBranch(record);
            BizObject.PurgeCacheItems("Branch");
            return ret;
        }

        public static bool DeleteBranch(int id)
        {
            bool ret = SiteProvider.BranchAnLoan.DeleteBranch(id);
            BizObject.PurgeCacheItems("Branch");
            return ret;
        }
        #endregion
    }
}
